﻿window.onscroll=function(){
  var currentItem="#item1";
  var collectH = document.documentElement.scrollTop || document.body.scrollTop;
  var windowHeight = window.innerHeight;
  var hrefItem = document.querySelector('#menu .current');
  var hrefvalue = hrefItem.getAttribute('href');
  var items=document.querySelectorAll('.item');
  items.forEach(i => {
    var offsetTop=i.offsetTop;
    console.log(offsetTop)
    if(collectH > offsetTop-10){
      currentItem = "#"+i.getAttribute('id');

    }
  })
  if(collectH+windowHeight > document.documentElement.offsetHeight-10){
    currentItem = "#item4";
  }
  if(currentItem != hrefvalue){
    hrefItem.classList.remove('current');
    document.querySelector("#menu a[href='"+currentItem+"']").classList.add('current');
  }
}